
package com.yimixian.app.fragment;

import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.EventBusDomain.CartItemRemoveMessage;
import com.yimixian.app.EventBusDomain.PlusMinusIconMessage;
import com.yimixian.app.EventBusDomain.ShowAdSelectChange;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.activity.YMXMainActivity;
import com.yimixian.app.adapter.CartRecylerStickyAdapter;
import com.yimixian.app.address.AddressViewForCart;
import com.yimixian.app.address.ChooseAddressOrStoreActivity;
import com.yimixian.app.address.ShowAddressesActivity;
import com.yimixian.app.cart.CartDetailActivity;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.cart.CartNewView;
import com.yimixian.app.cart.GoodsItemPresenter;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.common.UMengConstants;
import com.yimixian.app.common.WebViewActivity;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Address;
import com.yimixian.app.model.CartUpdate;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.User;
import com.yimixian.app.order.ConfirmOrderActivity;
import com.yimixian.app.rest.api.CartUpdateAPI;
import com.yimixian.app.ui.MessageDialogView;
import com.yimixian.app.ui.ParabolaView;
import com.yimixian.app.ui.PlusMinusIcon;
import com.yimixian.app.ui.dialog.CartDialog;
import com.yimixian.app.ui.wheelview.CartRadioButton;
import com.yimixian.app.util.DateUtil;
import com.yimixian.app.util.PaoWuXianAnimTool;
import com.yimixian.app.util.PaoWuXianAnimator;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.Strings;
import com.yimixian.app.util.UiUtils;
import com.ymx.sdk.widget.recyclerview.itemanimator.FlipInBottomXAnimator;
import com.ymx.sdk.widget.recyclerview.stickyheader.StickyHeaderDecoration;

import de.greenrobot.event.EventBus;

import org.json.JSONObject;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class CartFragment extends DialogGoodDetailFragment implements
        GoodsItemPresenter.OnPlusButtonClickListener {
    public static boolean isNeedAnimator = true;

    private CartRecylerStickyAdapter mCartRecylerAdapter;
    private Handler mHandler = new Handler();
    private CartRadioButton mCartTab;
    private CartManager mCartManager = CartManager.getInstance();
    public DataManager mDataManager;
    private LocalBroadcastManager mBroadcastManager;
    private SharedPreferences mSharedPreferences;
    private CartUpdateAPI mCartUpdateAPI;
    public ArrayList<GoodsItem> mGoodsItems;
    private int mRemovedPosition;
    private boolean isNeedRemove = false;
    private boolean synUnLoginDataSuccess = false;

    @InjectView(R.id.address_view)
    AddressViewForCart mAddressView;
    @InjectView(R.id.ll_root)
    RelativeLayout mRlRoot;
    @InjectView(R.id.lv_goodslist)
    RecyclerView goodsList;
    @InjectView(R.id.iv_no_login)
    ImageView mIvNoLogin;
    @InjectView(R.id.tv_login)
    TextView mTvLogin;
    @InjectView(R.id.rl_login)
    RelativeLayout mRlLogin;
    @InjectView(R.id.tv_button_txt)
    TextView mTvButtonTxt;
    @InjectView(R.id.order_no_data)
    View mWithoutGoodsView;
    @InjectView(R.id.cart_new_view)
    CartNewView mCartNewView;
    @InjectView(R.id.cannot_deliver_view)
    LinearLayout mCannotDeliverView;
    @InjectView(R.id.show_delivery_zone_button)
    TextView mShowDeliveryZoneButton;
    @InjectView(R.id.cannot_deliver_text)
    TextView mCannotDeliverText;
    @InjectView(R.id.content_view)
    LinearLayout mContentView;

    StickyHeaderDecoration mStickyHeaderDecoration;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cart, null);

        ButterKnife.inject(this, view);

        mGoodsItems = new ArrayList<>();
        mWithoutGoodsView.setVisibility(View.GONE);
        mAddressView.setVisibility(View.GONE);
        mCartNewView.setVisibility(View.GONE);
        goodsList.setVisibility(View.GONE);

        if(!isAdded()) {
            return view;
        }

        DateUtil.setCityUMeng(null, UMengConstants.NEW_CART_PV);

        if (getActivity() instanceof YMXMainActivity) {
            CartManager.getInstance().setCartQuantityTextView(
                    ((YMXMainActivity) getActivity()).mCartTotalQuantity);
        }

        // 设置购物车页面的 "选好了" 的点击事件
        CartNewView.Adapter adapter = new CartNewView.Adapter() {
            @Override
            public void onButtonClicked() {
                if (isAddressEmpty()) {
                    UiUtils.showToast(getString(R.string.no_address_tips));
                    return;
                }
                CartUpdate mCartUpdate = CartManager.getInstance().mCartUpdate;
                if (mCartUpdate != null && mCartUpdate.delieverGoodsGroup != null) {
                    if (mCartUpdate.delieverGoodsGroup.size() <= 0){
                        return;
                    }else if (mCartUpdate.delieverGoodsGroup.size() == 1){
                        long groupId = mCartUpdate.delieverGoodsGroup.get(0).groupId;
                        if (groupId <= 0) return;
                        MobclickAgent.onEvent(getActivity(), StatConstants.HOME_CART_SELECT_FINISH_CLICK);
                        getActivity().startActivityForResult(
                                new Intent(getActivity(), ConfirmOrderActivity.class).putExtra("group_id",
                                        groupId), Constants.CONFIRM_ORDER_REQUEST_CODE);
                    }else {
                        CartDialog cartDialog = new CartDialog(getActivity(), mCartUpdate);
//                        cartDialog.getWindow().setWindowAnimations(R.style.popup_in_center);
                        cartDialog.getWindow().setWindowAnimations(R.style.JieSuanStyle);
                        cartDialog.show();
                    }
                }
            }

            @Override
            public BaseAdapter getCartListAdapter() {
                return mCartManager.getGoodsAdapter(getActivity());
            }
        };
        mCartNewView.setAdapter(adapter);
        mCartManager.setCartNewView(mCartNewView);

        initWithoutGoodsView();
        if(isAdded()) {
            setDialogView(((CartDetailActivity)getActivity()).mDialogView);
        }

        updateGoodsIds();
        mCartRecylerAdapter = new CartRecylerStickyAdapter(mGoodsItems, getActivity());

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        goodsList.setLayoutManager(layoutManager);
        goodsList.setItemAnimator(new FlipInBottomXAnimator(new LinearInterpolator()) {
            @Override
            public void onRemoveFinished(RecyclerView.ViewHolder item) {
                super.onRemoveFinished(item);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mCartRecylerAdapter != null) {
                            mCartRecylerAdapter.notifyDataSetChanged();
                        }
                    }
                }, 500);
            }
        });
        goodsList.setAdapter(mCartRecylerAdapter);
        mStickyHeaderDecoration = new StickyHeaderDecoration(mCartRecylerAdapter);
        goodsList.addItemDecoration(mStickyHeaderDecoration);
        regAddressChangeFinishBroadcast();
        // address相关
        mDataManager = DataManager.getInstance();
        showAddress();
        mAddressView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                        UMengConstants.NEW_CART_CHANGE_ADDRESS_BTN);
                jumpAddress();
            }
        });
        return view;
    }

    public void updateGoodsIds() {
        if (mGoodsItems != null) {
            mGoodsItems.clear();
            addGoodsItem();
        } else {
            mGoodsItems = new ArrayList<>();
            addGoodsItem();
        }
    }

    private void addGoodsItem() {
        if (mCartManager.mGoodsGroup != null) {
            for (int i = 0; i < mCartManager.mGoodsGroup.size(); i++) {
                if (mCartManager.mGoodsGroup.get(i) != null && mCartManager.mGoodsGroup.get(i).goods != null) {
                    for (int j = 0; j < mCartManager.mGoodsGroup.get(i).goods.size(); j++) {
                        if (mCartManager.mGoodsGroup.get(i).goods.get(j) != null) {
                            mGoodsItems.add(mCartManager.mGoodsGroup.get(i).goods.get(j));
                        }
                    }
                }
            }
        }
    }

    /**
     * 处理是否显示空页面
     */
    private void dealWithoutView() {
        if (CartManager.getInstance().mTotalQuantity == 0) {
            if(isAdded() && getActivity() instanceof CartDetailActivity) {
                if(((CartDetailActivity)getActivity()).mEditCartView != null) {
                    ((CartDetailActivity) getActivity()).mEditCartView.setVisibility(View.GONE);
                }
            }
        } else {
            mWithoutGoodsView.setVisibility(View.GONE);
            if (mAddressView != null) {
                mAddressView.setVisibility(View.VISIBLE);
            }
            if (mCartNewView != null) {
                mCartNewView.setVisibility(View.VISIBLE);
            }
            if (goodsList != null) {
                goodsList.setVisibility(View.VISIBLE);
            }
            if(isAdded() && getActivity() instanceof CartDetailActivity) {
                if(((CartDetailActivity)getActivity()).mEditCartView != null) {
                    ((CartDetailActivity) getActivity()).mEditCartView.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    /**
     * 初始化没有商品的view
     */
    public void initWithoutGoodsView() {
        mIvNoLogin.setImageResource(R.drawable.ic_no_cart_data);
        mTvLogin.setText(getString(R.string.cart_no_data_tips));
        mTvButtonTxt.setText(getString(R.string.go_home));
        mRlLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isAdded()) {
                    startActivity(YMXMainActivity.buildIntentTab(getActivity(),
                        YMXMainActivity.TAB_HOME));
                }
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Override
    public void onPlusButtonClick(View plusView, Point startPoint, Bitmap animBack) {
        if (getActivity() instanceof YMXMainActivity) {

            PaoWuXianAnimTool.startAnim(getActivity(), plusView, mCartTab, animBack, PaoWuXianAnimTool.DEFAULT_DURATION);

            if(true)
                return;

            ParabolaView parabolaView = new ParabolaView(getActivity(), animBack);
            /*parabolaView.setAnimListener(new ParabolaView.AnimListener() {
                @Override
                public void onEndAnim() {
                    ((YMXMainActivity) getActivity()).initCartViewAnim();
                }
            });*/
            mRlRoot.addView(parabolaView);
            parabolaView.startAnimation(mHandler, mCartTab.getCartLocationPoint(), startPoint);
        }
    }

    public void jumpAddress() {

        Intent intent = new Intent(SystemFramework.getInstance().getGlobalContext(),
                ChooseAddressOrStoreActivity.class);
        intent.putExtra(ShowAddressesActivity.EXTRA_MODE, ShowAddressesActivity.PICK_ADDRESS_MODE);
        startActivityForResult(intent, Constants.PICK_ADDRESS_REQUEST_CODE);

    }

    public boolean isAddressEmpty() {
        Address mAddress = SharedPreferencesHelper.getAddress(SharePreferenceKeys.CURRENT_ADDRESS);
        if (mAddress == null || mAddress.id == 0) {
            return true;
        } else {
            return false;
        }
    }

    public void regAddressChangeFinishBroadcast() {
        mBroadcastManager = LocalBroadcastManager.getInstance(SystemFramework
                .getInstance().getGlobalContext());
        IntentFilter filterAddressChange = new IntentFilter();
        filterAddressChange
                .addAction(Constants.BROADCAST_RECEIVE_ADDRESS_CHANGE_FINISH);
        filterAddressChange.addAction(Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST);
        mBroadcastManager.registerReceiver(mReceiver,
            filterAddressChange);
    }

    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST.equals(intent.getAction())) {
                int state = intent.getExtras().getInt(Constants.GOOD_ID);
                if (state == 0) {
                    showCountLimitDialog();
                } else {
                    float price = intent.getExtras().getFloat(Constants.ITEM_GIGTTYPE_MIN_PRICE);
                    showMoneyLimitDialog(price);
                }
            }
        }
    };

    @Override
    public void onDestroy() {
        if (mReceiver != null && mBroadcastManager != null) {
            mBroadcastManager.unregisterReceiver(mReceiver);
        }
        super.onDestroy();
    }

    public void showAddress() {
        if (isAddressEmpty()) {
            mAddressView.showNodata();
        } else {
            if (mDataManager == null) {
                return;
            }
            Address mAddress = SharedPreferencesHelper
                    .getAddress(SharePreferenceKeys.CURRENT_ADDRESS);
            if (mAddress != null && !mAddress.isPickup) {
                mAddressView.bind(mAddress);
            } else {
                User user = mDataManager.<User> get(CacheKeys.CURRENT_USER);
                if (user != null) {
                    mAddressView.bind(SharedPreferencesHelper.getPickUpStoreName(), user.tel);
                }
            }
        }
    }

    @Override
    public void onResume() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().registerSticky(this);
        }
        if (DataManager.getInstance().get(CacheKeys.TOKEN) != null) {
            getCartData();
        }
        super.onResume();
    }

    private void getCartData() {
        fetchData(true, CartUpdateAPI.CART_UPDATE_API);
        try {
            if (mCartUpdateAPI != null) {
                mCartUpdateAPI.getCartInfo();
                showTransDialog(null, true);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void onEventMainThread(String message) {
        if (message != null) {
            if (Constants.EVENT_BUS_CART_FRAGMENT_REFRESH.equals(message)) {
                if (mCartRecylerAdapter != null) {
                    mCartRecylerAdapter.notifyDataSetChanged();
                }
            }
            // 取消掉Loading框
            else if (Constants.EVENT_BUS_DISSMISS_DIALOG.equals(message)) {
                hideDialog();
                hidePopupWindow();
                if (mPlusMinusIcon != null) {
                    mPlusMinusIcon.setButtonCanClick(true);
                }
                EventBus.getDefault().removeStickyEvent(message);
            }
            //刷新UI
            else if(Constants.REFRESH_UI.equals(message)) {
                if(mCartRecylerAdapter != null) {
                    mCartRecylerAdapter.notifyDataSetChanged();
                }
            }
            /*else if(Constants.EVENT_BUS_GET_ADDRESS_SUCCESS_MESSAGE.equals(message)
                &&mCartUpdateAPI!=null&&!mCartUpdateAPI.hasNeedsParams){//上次调用接口由于还没有需要参数导致没有调接口的话进入下面方法
                getCartData();
            }*/
            else if(Constants.SYNC_UNLOGIN_DATA_SUCCESS.equals(message)){
                mWithoutGoodsView.setVisibility(View.VISIBLE);
                synUnLoginDataSuccess = false;
                dealWithCartUpdate(CartManager.getInstance().mCartUpdate);
                updateGoodsIds();
                if(mGoodsItems.size()>0){
                    mWithoutGoodsView.setVisibility(View.GONE);
                    synUnLoginDataSuccess = true;
                    if(mCartRecylerAdapter!=null)mCartRecylerAdapter.notifyDataSetChanged();
                }
                EventBus.getDefault().removeStickyEvent(message);
                if (mPlusMinusIcon != null) {
                    mPlusMinusIcon.setButtonCanClick(true);
                }
                dealWithoutView();
                hideDialog();
                hidePopupWindow();
            }
        }
    }

    private PlusMinusIcon mPlusMinusIcon;

    // 发起商品 + / - 的请求
    public void onEventMainThread(PlusMinusIconMessage message) {
        if (message != null) {
            mPlusMinusIcon = message.icon;
            if (Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST.equals(message.message)) {
                if (mPlusMinusIcon != null) {
                    fetchData(true, CartUpdateAPI.CART_UPDATE_API);
                    mCartUpdateAPI.mGoodId = mPlusMinusIcon.mGoodsItem.id;
                    if (Constants.PLUS_BUTTON_CLICKED_BROADCAST
                            .equals(mPlusMinusIcon.mButtonState)) {
                        showPopupWindow(getActivity(), R.id.popupwindow_container);
                        mCartUpdateAPI.mTrend = 1;
                        mCartUpdateAPI.getCartUpdate(1);
                    } else if (Constants.MINUS_BUTTON_CLICKED_BROADCAST
                            .equals(mPlusMinusIcon.mButtonState)) {
                        mCartUpdateAPI.mTrend = 0;
                        if (mPlusMinusIcon.getQuantity() - 1 == 0) {
                            if (isAdded()) {
                                final MessageDialogView messageDialogView = new MessageDialogView(
                                        getActivity());
                                messageDialogView.bindConfirm("提示", "我真的很好哦~\n确定要删除么？",
                                    new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            showPopupWindow(getActivity(),
                                                R.id.popupwindow_container);
                                            // 执行删除的指令
                                            // mCartUpdateAPI.getCartUpdate();
                                            isNeedRemove = true;
                                            CartItemRemoveMessage message1 = new CartItemRemoveMessage();
                                            message1.goodsId = mPlusMinusIcon.mGoodsItem.id;
                                            int position = 0;
                                            if (mGoodsItems != null && mGoodsItems.size() > 0) {
                                                for (int i = 0; i < mGoodsItems.size(); i++) {
                                                    if (mGoodsItems.get(i) != null
                                                        && mGoodsItems.get(
                                                        i).id == mPlusMinusIcon.mGoodsItem.id) {
                                                        position = i;
                                                        break;
                                                    }
                                                }
                                            }
                                            message1.position = position;
                                            EventBus.getDefault().post(message1);
                                            ((CartDetailActivity) getActivity()).mDialogView.removeView(messageDialogView);
                                        }
                                    }, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            if (mPlusMinusIcon != null) {
                                                mPlusMinusIcon.setButtonCanClick(true);
                                            }
                                            ((CartDetailActivity) getActivity()).mDialogView.removeView(messageDialogView);
                                        }
                                    });
                                UiUtils.setHierarchyChangeListener(mDialogView, getActivity());
                                ((CartDetailActivity)getActivity()).mDialogView.addView(messageDialogView);
                            }
                        } else {
                            showPopupWindow(getActivity(), R.id.popupwindow_container);
                            mCartUpdateAPI.getCartUpdate(1);
                        }
                    }
                }
            }
        }
    }

    /**
     * 用来接收删除商品的操作
     */
    public void onEventMainThread(final CartItemRemoveMessage message) {
        if (message != null && message.goodsId != 0) {
            if (mCartUpdateAPI == null) {
                mCartUpdateAPI = new CartUpdateAPI(this);
            }
            if (isAdded()) {
                if (isNeedRemove) {
                    mCartUpdateAPI.mRemoveGoodId = message.goodsId;
                    mRemovedPosition = message.position;
                    mCartUpdateAPI.removeCartItem();
                    isNeedRemove = false;
                } else {
                    final MessageDialogView messageDialogView = new MessageDialogView(
                        getActivity());
                    messageDialogView.bindConfirm("提示", "我真的很好哦~\n确定要删除么？",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showPopupWindow(getActivity(), R.id.popupwindow_container);
                                mCartUpdateAPI.mRemoveGoodId = message.goodsId;
                                mRemovedPosition = message.position;
                                mCartUpdateAPI.removeCartItem();
                                ((CartDetailActivity) getActivity()).mDialogView.removeView(messageDialogView);
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ((CartDetailActivity) getActivity()).mDialogView.removeView(messageDialogView);
                            }
                        });
                    UiUtils.setHierarchyChangeListener(mDialogView, getActivity());
                    ((CartDetailActivity)getActivity()).mDialogView.addView(messageDialogView);
                }
            }
        }
    }
    @Override
    public void initRequestAPI(String requestKeys) {
        if (requestKeys != null && CartUpdateAPI.CART_UPDATE_API.equals(requestKeys)
                && mCartUpdateAPI == null) {
            mCartUpdateAPI = new CartUpdateAPI(this);
        }
    }

    @Override
    public void onHttpSuccess(String url, JSONObject responseJson) {
        mWithoutGoodsView.setVisibility(View.VISIBLE);
        if (url.contains(CartUpdateAPI.CART_GET)) {
            if(synUnLoginDataSuccess){
                synUnLoginDataSuccess = false;
            }else{
                CartUpdate cartUpdate = mCartUpdateAPI.parseJson(responseJson);
                dealWithCartUpdate(cartUpdate);
                CartManager.getInstance().updateCartGoods(cartUpdate);
                if (mCartRecylerAdapter != null) {
                    updateGoodsIds();
                    if (mCartRecylerAdapter != null) mCartRecylerAdapter.notifyDataSetChanged();
//                isClearStickyHeaderDecoration(cartUpdate);
                }
            }
        }
        if (url.contains(CartUpdateAPI.CART_UPDATE)) {
            CartUpdate cartUpdate = mCartUpdateAPI.parseJson(responseJson);
            CartManager.getInstance().updateCartGoods(cartUpdate, mPlusMinusIcon);
            updateGoodsIds();
            if (mCartRecylerAdapter != null) mCartRecylerAdapter.notifyDataSetChanged();
        }
        if (url.contains(CartUpdateAPI.CART_REMOVE)) {
            CartUpdate cartUpdate = mCartUpdateAPI.parseJson(responseJson);
            CartManager.getInstance().updateCartGoods(cartUpdate);
            if (mCartRecylerAdapter != null) {
                if (mCartRecylerAdapter != null) {
                    mCartRecylerAdapter.removeItemByPosition(mRemovedPosition);
                }
                updateGoodsIds();
//                isClearStickyHeaderDecoration(cartUpdate);
            }
        }
        if (mPlusMinusIcon != null) {
            mPlusMinusIcon.setButtonCanClick(true);
        }
        dealWithoutView();
        hideDialog();
        hidePopupWindow();
        synUnLoginDataSuccess = false;
    }

    public void dealWithCartUpdate(CartUpdate cartUpdate){
        if (cartUpdate != null && cartUpdate.store != null) {
            if (!TextUtils.isEmpty(cartUpdate.store.cannotDeliverText)) {
                hideDialog();
                if (mContentView != null && mWithoutGoodsView != null) {
                    mContentView.setVisibility(View.GONE);
                    mWithoutGoodsView.setVisibility(View.GONE);
                }
                final String deliveryZoneUrl = cartUpdate.store.deliveryZoneUrl;
                if (mCannotDeliverView != null) {
                    mCannotDeliverView.setVisibility(View.VISIBLE);
                    mWithoutGoodsView.setVisibility(View.GONE);
                }
                if (!Strings.isNullOrEmpty(cartUpdate.store.cannotDeliverText)
                    && mCannotDeliverText != null) {
                    mCannotDeliverText.setText(cartUpdate.store.cannotDeliverText);
                    mWithoutGoodsView.setVisibility(View.GONE);
                }
                if (mShowDeliveryZoneButton != null) {
                    if (Strings.isNullOrEmpty(cartUpdate.store.deliveryZoneUrl)) {
                        mShowDeliveryZoneButton.setVisibility(View.GONE);
                    } else {
                        mShowDeliveryZoneButton.setVisibility(View.VISIBLE);
                        mShowDeliveryZoneButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                MobclickAgent.onEvent(getActivity(),
                                    StatConstants.HOME_CANNOT_DELIVERY_CLICK);
                                Intent intent = new Intent(getActivity(),
                                    WebViewActivity.class);
                                intent.putExtra(WebViewActivity.EXTRA_URL, deliveryZoneUrl);
                                startActivity(intent);
                            }
                        });
                    }
                }
                return;
            } else {
                if (mContentView != null && mCannotDeliverView != null) {
                    mContentView.setVisibility(View.VISIBLE);
                    mCannotDeliverView.setVisibility(View.GONE);
                }
            }
        }
    }

//    //对列表是否分组进行逻辑处理
//    public void isClearStickyHeaderDecoration(CartUpdate cartUpdate){
//        if (cartUpdate == null || cartUpdate.delieverGoodsGroup == null) return;
//        if (cartUpdate.delieverGoodsGroup.size() <= 1){
//            if (mStickyHeaderDecoration != null)
//                goodsList.removeItemDecoration(mStickyHeaderDecoration);
//            mStickyHeaderDecoration = null;
//        }else{
//            if (mStickyHeaderDecoration == null){
//                mStickyHeaderDecoration = new StickyHeaderDecoration(mCartRecylerAdapter);
//                goodsList.addItemDecoration(mStickyHeaderDecoration);
//            }
//        }
////        mCartRecylerAdapter.notifyDataSetChanged();
//    }

    @Override
    public void OnHttpFailure(String url, String errorMessage, boolean errorFromUs) {
        if (mPlusMinusIcon != null) {
            mPlusMinusIcon.setButtonCanClick(true);
        }
        if (errorFromUs) {
            if (url.contains(CartUpdateAPI.CART_UPDATE)) {
                getCartData();
                EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_GET_CART));
            }
        }
        hideDialog();
        hidePopupWindow();
        synUnLoginDataSuccess = false;
    }

    @Override
    public void fetchDataFromNetwork(String key) {
        if (CartUpdateAPI.CART_UPDATE_API.equals(key)) {
            initRequestAPI(key);
        }
    }

    public void onEventMainThread(ShowAdSelectChange event) {
        showAddress();
    }

    /**
     * 删除按钮点击之后刷新列表
     */
    public void updateGoodsView() {
        if (mCartRecylerAdapter != null) {
            isNeedAnimator = true;
            mCartRecylerAdapter.notifyDataSetChanged();
            if(mHandler != null) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isNeedAnimator = false;
                    }
                }, 500);
            }
        }
    }
}
